/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xinxi.dms.view;

import com.xinxi.dms.po.Stay;
import com.xinxi.dms.po.Student;
import com.xinxi.dms.service.CheckoutServiceIMPL;
import com.xinxi.dms.service.ICheckoutService;
import com.xinxi.dms.util.FrameUtil2;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author zhigu
 */
public class CheckoutFrame_25 extends javax.swing.JInternalFrame {
    ICheckoutService checkoutService = new CheckoutServiceIMPL();
    /**
     * Creates new form CheckoutFrame_25
     */
    public CheckoutFrame_25() {
        initComponents();
        //获取表格的列
        TableColumn tabColumn = this.tabStay.getColumnModel().getColumn(8);
        //设置宽高
        tabColumn.setWidth(0);
        
        tabColumn.setMinWidth(0);
        
        tabColumn.setMaxWidth(0);
        //设置表格的表头 指的就是上面对应的要隐藏的列
        tabStay.getTableHeader().getColumnModel().getColumn(8).setMaxWidth(0);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtRoomno = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabStay = new javax.swing.JTable();
        btnSearch = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("25号楼退宿系统");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        jLabel1.setText("房间号：");

        tabStay.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "学生编号", "学生姓名", "学生性别", "学生院校", "楼号", "单元号", "房间号", "入住状态", "房间编号"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabStay.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabStayMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabStay);

        btnSearch.setText("查询");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 0, 0));
        jLabel3.setText("提示：双击表格中未入住的同学即可退宿成功");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(139, 139, 139)
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(txtRoomno, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnSearch)))
                .addContainerGap(319, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtRoomno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(btnSearch))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(84, 84, 84))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
////
    private void tabStayMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabStayMouseClicked
        // TODO add your handling code here:
        //获取点击次数
        int clickCount = evt.getClickCount();
        if (clickCount == 2) {
            int row = this.tabStay.getSelectedRow();
            int stuid = (Integer) this.tabStay.getValueAt(row, 0);
            //根据学生编号查询当前学生状态
            Student stu = checkoutService.getState(stuid);
            if (stu.getState().equals("未入住")) {
                JOptionPane.showMessageDialog(this, "该名同学还没有入住，不能退宿");
            }else if(stu.getState().equals("已退宿")){
                JOptionPane.showMessageDialog(this, "该名同学已经退宿，请不要重复退宿");
            } else {
                int roomid = (Integer) this.tabStay.getValueAt(row, 8);
                boolean result = checkoutService.checkout(stuid, roomid);
                if (result) {
                    JOptionPane.showMessageDialog(this, "退宿成功");
                    String roomno = this.txtRoomno.getText();
                    List<Stay> list = checkoutService.findAll_25(roomno);
                    refresh(list);
                } else {
                    JOptionPane.showMessageDialog(this, "退宿失败");
                }
            }
        }
    }//GEN-LAST:event_tabStayMouseClicked

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        // TODO add your handling code here:
        String roomno = txtRoomno.getText();
        List<Stay> list = checkoutService.findAll_25(roomno);
        refresh(list);
    }//GEN-LAST:event_btnSearchActionPerformed

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        // TODO add your handling code here:
        FrameUtil2.framemap.remove(CheckoutFrame_25.class.getName());
    }//GEN-LAST:event_formInternalFrameClosing


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSearch;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabStay;
    private javax.swing.JTextField txtRoomno;
    // End of variables declaration//GEN-END:variables

    private void refresh(List<Stay> list) {
         DefaultTableModel dtm = (DefaultTableModel) this.tabStay.getModel();
        while(dtm.getRowCount()>0){
            //从第0行开始删除
            dtm.removeRow(0);
        }//遍历集合
        for(Stay s: list){
            Vector v =new Vector();
            v.add(s.getStuid());
            v.add(s.getStuname());
            v.add(s.getStusex());
            v.add(s.getStuschool());
            v.add(s.getBuildingno());
            v.add(s.getUnitno());
            v.add(s.getRoomno());
            v.add(s.getState());
            v.add(s.getRoomid());
            dtm.addRow(v);
        }
    }
}
